<script setup>
import { ref, onMounted, reactive } from "vue";
import { yuangongIndex, yuangongSave, yuangongDelete } from "@/api";
import tableValue from "@/components/tableValue.vue";

// 传递子组件表格数据
const tableData = reactive([]);

// 传递子组件的列配置数组
const columns = [
  { prop: "id", label: "员工编号", width: "100" },
  { prop: "name", label: "姓名", width: "150" },
  { prop: "ruzhitime", label: "入职时间", width: "150" },
  { prop: "sex", label: "性别", width: "150" },
  { prop: "year", label: "年龄", width: "150" },
  { prop: "zhiwu", label: "职务", width: "150" },
  { prop: "remark", label: "联系电话", width: "150" },
  // 操作列（特殊列，无 prop）
  { label: "操作", type: "action" },
];

const formRef = ref();
// 信息条数
const total = ref(1);
// 搜索的字段信息
const SearchValue = ref("");

onMounted(async () => {
  hyglTable();
});

// 请求会员数据
const hyglTable = async (names = "", page = null) => {
  const res = await yuangongIndex({
    name: names,
    pageNum: page || 1,
    pageSize: 5,
  });
  // console.log(names);

  total.value = res.total;
  tableData.length = 0;
  Object.assign(tableData, res.data);
  // console.log(total.value);
};
// console.log(total.value);

// 搜索功能
const searchType = (value) => {
  SearchValue.value = value;
  hyglTable(value.value);
};

// 新增功能
const dialogFormVisible = ref(false);

// 判断是新增还是编辑
const RowType = ref("");

// 点击了新增按钮触发
const adds = async () => {
  RowType.value = "员工新增";
  form.value.sex = "男";
  form.value.zhiwu = "健身教练";
  dialogFormVisible.value = true;
};
// 默认表单原始数据
// const initialForm = reactive({
// id: "1";
// name: "邓为1";
// remark: "123333";
// ruzhitime: "2023-11-16";
// sex: "男";
// year: "33";
// zhiwu: "健身教练";
// });
// const form = ref({ ...initialForm });

// 表单数据
const form = ref({});

const delshow = ref(false); // 用show变量控制删除弹窗，默认关闭
// 点击了确定按钮触发,会员新增
const submit = () => {
  // console.log("表单提交");
  formRef.value.validate(async (valid) => {
    if (valid) {
      dialogFormVisible.value = false;
      console.log("表单验证通过");
      if (RowType.value === "员工新增") {
        const res = await yuangongSave(form.value);
        if (res.code === 200) ElMessage.success(res.msg);
      } else {
        const res = await yuangongSave(form.value);
        if (res.code === 200) ElMessage.success(res.msg);
      }
      hyglTable("", pagess.value);

      // 处理表单数据
      formRef.value.resetFields();
      form.value = {};
    } else {
      console.log("表单验证失败");
      return false;
    }
  });
};
// 编辑
const editType = (item) => {
  RowType.value = "员工编辑";
  dialogFormVisible.value = true;
  Object.assign(form.value, item);
};

// 会员删除
const delId = ref();
const deletes = (item) => {
  delId.value = item.id;
  delshow.value = true;
};
const handleConfirm = async () => {
  const res = await yuangongDelete({ id: delId.value });
  if (res.code === 200) ElMessage.success(res.msg);
  delshow.value = false;
  hyglTable("", pagess.value);
};
// 表单校验
const rules = reactive({
  name: [
    { required: true, message: "输入不能为空", trigger: "blur" },
    { min: 2, max: 5, message: "请输入2-5个字符", trigger: "blur" },
  ],
  ruzhitime: [{ required: true, message: "输入不能为空", trigger: "blur" }],
  remark: [
    { required: true, message: "请输入手机号码", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "请输入正确的手机号码",
      trigger: "blur",
    },
  ],
  year: [
    { required: true, message: "输入不能为空", trigger: "blur" },
    { min: 0, message: "请输入合理的年龄", trigger: "blur" },
  ],
});
const pagess = ref(0);

const Page = async (pages) => {
  await hyglTable("", pages);
  pagess.value = pages;
};
</script>

<template>
  员工管理
  <tableValue
    :textList="tableData"
    :columns="columns"
    :total="Number(total)"
    @SearchValue="searchType"
    @add="adds"
    @edit="editType"
    @delete="deletes"
    @PageType="Page"
  />

  <el-dialog v-model="dialogFormVisible" :title="RowType" width="800">
    <el-form :model="form" ref="formRef" :rules="rules">
      <el-form-item label="名 字： " prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="入职时间： " prop="ruzhitime">
        <el-date-picker
          v-model="form.ruzhitime"
          type="date"
          placeholder="Pick a day"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="性 别： " prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio value="男" size="large">男</el-radio>
          <el-radio value="女" size="large">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="电 话： " prop="remark">
        <el-input v-model="form.remark" autocomplete="off" />
      </el-form-item>
      <el-form-item label="年 龄： " prop="year">
        <el-input v-model="form.year" autocomplete="off" />
      </el-form-item>
      <el-form-item label="职 务： " prop="zhiwu">
        <el-radio-group v-model="form.zhiwu">
          <el-radio value="健身教练" size="large">健身教练</el-radio>
          <el-radio value="保洁员" size="large">保洁员</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="delshow" title="会员删除" width="500" align-center>
    <span>此操作将永久删除该数据，删除后不可恢复，确定要继续吗？</span>
    <template #footer>
      <el-button @click="delshow = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>
<style scoped lang="scss"></style>
